package com.j180.demo.controller.student;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.j180.demo.bean.PageBean;
import com.j180.demo.bean.ResultBean;
import com.j180.demo.bean.StudentBean;
import com.j180.demo.service.student.IStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student")
@Api("学生操作控制器")
public class StudentController {
     @Resource
    private IStudentService iStudentService;

     @ApiOperation("根据条件查询学生信息")
     @GetMapping("/show")
    public ResultBean  showPageInfo(@ApiParam("查询条件")@RequestBody Map<String,String> map){ System.out.println(map);
        PageBean<List<StudentBean>> page = iStudentService.showStudengInfoByCondition(map);
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(0);
        resultBean.setMsg("查询成功");
        resultBean.setDatas(page);
        return resultBean;
    }

    @ApiOperation(value = "添加学生",consumes = "application/json")
    @PostMapping("/add")
    public ResultBean addStudentInfo(@ApiParam("学生对象")@RequestBody StudentBean studentBean){

        ResultBean resultBean = new ResultBean();

        int num =  iStudentService.addStudentInfo(studentBean);

        if(num!=0){
            resultBean.setCode(0);
            resultBean.setMsg("添加成功");
        }else{
            resultBean.setCode(1);
            resultBean.setMsg("添加失败");
        }

            return resultBean;
        }

        @ApiOperation("删除学生信息")
        @DeleteMapping("/delete/{id}")
        public ResultBean deleteStudent(@ApiParam("学生编号")@PathVariable("id") int id){

            ResultBean resultBean = new ResultBean();

            int num =  iStudentService.deleteStudentInfo(id);

            if(num!=0){
                resultBean.setCode(0);
                resultBean.setMsg("删除成功");

            }else{
                resultBean.setCode(1);
                resultBean.setMsg("删除失败");
            }

            return resultBean;
        }
     @ApiOperation("显示学生详细信息")
    @GetMapping("/show/{id}")
    public ResultBean  showStduentInfo(@ApiParam("学生编号")@PathVariable("id") Integer id){

        ResultBean<StudentBean> resultBean = new ResultBean();
       StudentBean studentBean = iStudentService.showStudentInfoDetail(id);

   if(studentBean!=null){
       resultBean.setCode(0);
       resultBean.setMsg("查询成功");
       resultBean.setDatas(studentBean);
   }else{
       resultBean.setCode(1);
       resultBean.setMsg("查询失败");
   }
        return resultBean;
    }


    @ApiOperation(value = "修改学生信息",consumes = "application/json")
    @PutMapping("/update")
    public ResultBean deleteStudent(@ApiParam("学生对象")@RequestBody StudentBean studentBean){

        ResultBean resultBean = new ResultBean();

        int num =  iStudentService.updateStudengInfo(studentBean);

        if(num!=0){
            resultBean.setCode(0);
            resultBean.setMsg("修改成功");
        }else{
            resultBean.setCode(1);
            resultBean.setMsg("修改失败");
        }
        return resultBean;
    }


}
